<template>
  <div class="me_search">
    <el-dialog
      title="添加人员"
      :visible.sync="SearchPersonnelData.dialogVisible"
      class="small"
      height="50%"
      :before-close="handleClose"
      :modal-append-to-body="false"
    >
      <div class="box">
        <div class="left">
          <div class="left-top">
            <div class="inputDeep">
              <!--             :select="handleSelect"
                :fetch-suggestions="querySearchAsync" -->
              <el-autocomplete
                v-model="input"
                :fetch-suggestions="querySearchAsync"
                placeholder="搜索名字"
                @select="handleSelect"
              />
            </div>
            <div class="button">
              <el-tag
                v-for="(tag, index) in dataList"
                :key="index"
                closable
                :disable-transitions="false"
                class="tag"
                @close="handleCloses(tag)"
              >
                {{ tag.label }}
              </el-tag>
            </div>
          </div>
          <div class="left-but">
            <el-button type="primary" @click="confirm">确 定</el-button>
            <el-button @click="SearchPersonnelData.dialogVisible = false"
              >取 消</el-button
            >
          </div>
        </div>

        <!-- ------------------------ -->
        <div class="right">
          <div>
            <el-breadcrumb separator-class="el-icon-arrow-right">
              <el-breadcrumb-item
                v-for="(item, index) in breadcrumb"
                :key="item.id"
                ><span @click="goTo(item.id, index)">{{
                  item.label
                }}</span></el-breadcrumb-item
              >
            </el-breadcrumb>
            <el-table
              ref="tableData"
              height="35vh"
              :data="getData"
              style="width: 100%"
              :show-header="false"
              @cell-click="click"
              @selection-change="handleSelectionChange"
            >
              <el-table-column
                :selectable="checkboxT"
                type="selection"
                width="55"
              />
              <el-table-column>
                <template slot-scope="scope">
                  <div class="templateClass">
                    <span>
                      {{ scope.row.label }}
                    </span>
                    <i
                      v-if="scope.row.children != null"
                      class="iconfont el-icon-allxiayiji i4"
                    />
                  </div>
                  <!-- <i class="el-icon-arrow-down"></i> -->
                </template>
              </el-table-column>
            </el-table>
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer" />
    </el-dialog>
  </div>
</template>

<script>
import '@/styles/search.scss'
import { getsearch } from '@/api/uc/search'
import { searchUserByKeyword } from '@/api/uc/user.js'

export default {
  components: {},
  props: ['SearchPersonnelData'],
  data() {
    return {
      dialogVisible: false,
      classifyList: [
        { id: '2', name: '公司部门', icon: 'el-icon-alltuanduiguanli' }
      ],
      groupUserTree: [],
      roleUserTree: [],
      getData: [],
      isActive: true,
      dataList: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      permissionIds: [],
      input: '',
      breadcrumb: [],
      terrData: [],
      checkedGh: [],
      restaurants: [],
      timeout: null,
      teamUserTree: []
    }
  },
  watch: {
    SelectPersonnelData(val) {
      console.log(val, 'SelectPersonnelData')
    }
  },
  mounted() {
    this.getSearch()
  },
  methods: {
    get() {
      console.log(this.data)
    },
    getMsgFormSon(data) {
      this.dataList = data
    },
    handleClose(done) {
      this.dataList = ''
      this.$confirm('确认关闭？')
        .then((_) => {
          done()
        })
        .catch((_) => {})
    },
    //
    // 搜索
    querySearchAsync(queryString, cb) {
      console.log(queryString)
      // if (Verify(queryString) === true) {
      //   return
      // }
      // clearTimeout(this.timeout)
      setTimeout(() => {
        searchUserByKeyword({
          status: 0,
          pageIndex: 0,
          pageSize: 1,
          userName: queryString
        }).then((res) => {
          res.data.forEach((item) => {
            item.value = item.userName
          })
          cb(res.data)
        })
      }, 1000 * Math.random())
    },
    handleSelect(item) {
      this.input = ''
      this.dataList = []
      this.dataList.push({ id: item.userId, label: item.userName })
      this.setSelected(item.userId)
    },
    setSelected(id) {
      this.getData.forEach((e) => {
        if (e.id === id) {
          this.$refs.tableData.toggleRowSelection(e, true)
        }
      })
    },
    // 获取数据
    async getSearch() {
      const getDataList = await getsearch()
      this.getData = getDataList.data.groupUserTree[0].children
      this.terrData = Array.from(getDataList.data.groupUserTree[0].children)
      this.breadcrumb.push(getDataList.data.groupUserTree[0])
    },
    // tag回调
    handleCloses(tag) {
      this.$refs.tableData.toggleRowSelection()
      this.dataList.splice(this.dataList.indexOf(tag), 1)
    },
    click(row) {
      if (row.children) {
        this.getData = row.children
        this.breadcrumb.push({ label: row.label, id: row.id })
      }
    },

    checkboxT(row) {
      if (row.person === false) {
        return false
      } else {
        return true
      }
    },
    handleSelectionChange(selection) {
      console.log(selection)
      this.dataList = selection
      if (selection.length > 1) {
        this.$refs.tableData.clearSelection()
        this.$refs.tableData.toggleRowSelection(selection.pop())
      }
    },
    goTo(id, index) {
      const idl = id
      this.onId(idl, this.terrData)
      var flag = true
      for (var i = this.breadcrumb.length - 1; i >= 0; i--) {
        if (this.breadcrumb[i].id === idl) {
          flag = false
        }
        if (flag) {
          this.breadcrumb.splice(i, 1)
        }
      }
    },
    // 递归树
    onId(id, data) {
      for (const dat in data) {
        const e = data[dat]
        if (e.id == id) {
          this.getData = e.children
          return
        } else if (id == this.breadcrumb[0].id) {
          this.getData = this.terrData
        }
        if (e.children) {
          this.onId(id, e.children)
        }
      }
    },
    confirm() {
      this.SearchPersonnelData.dialogVisible = false
      this.$emit('dataListData', this.dataList)
    }
  }
}
</script>

<style scoped>
</style>
